﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class WebLog
    {
        DbCommon.DbHelper dbHelper = new DbCommon.DbHelper();
        /// <summary>
        /// 添加一条日志
        /// </summary>
        /// <param name="webLog">webLog的模型</param>
        /// <returns>返回添加的行数</returns>
        public int AddSingleLog(Model.WebLog webLog) {
            int i;
            SqlParameter[] pr = new SqlParameter[]{
                new SqlParameter("@UserName",webLog.UserName),
                new SqlParameter("@ManageClass",webLog.ManageClss),
                new SqlParameter("@IpAddress",webLog.IpAddress),
                new SqlParameter("@Title",webLog.Title)
            };
            string sqlstr = "insert into WebLog (WbUserName,WbManageClass,WbIpAddress,WbTitle) values(@UserName,@ManageClass,@IpAddress,@Title)";
            i = dbHelper.ExcuteNoQuery(sqlstr, pr);
            return i;
        }
        /// <summary>
        /// 得到日志列表
        /// </summary>
        /// <returns>日志列表</returns>
        public DataTable GetLoglist() {
            string sqlstr = "select * from WebLog";
            DataTable dt = dbHelper.ExcuteQuery(sqlstr);
            return dt;
        }
        /// <summary>
        /// 根据条件来获得几行数据
        /// </summary>
        /// <param name="rowNb">行数</param>
        /// <param name="sqlcon">条件</param>
        /// <param name="orderCon">排序条件</param>
        /// <returns>相应的几行数据</returns>
        public DataTable getLogRows(int rowNb, string sqlcon, string orderCon) {
            StringBuilder stb = new StringBuilder();
            stb.Append("select top ");
            stb.Append(rowNb);
            stb.Append(" * from WebLog where 1=1 and ");
            if (!string.IsNullOrEmpty(sqlcon)) {
                stb.Append(sqlcon);
            }
            if (!string.IsNullOrEmpty(orderCon)) {
                stb.Append(" " + orderCon);
            }
            DataTable dt = dbHelper.ExcuteQuery(stb.ToString());
            return dt;
            
        }
        /// <summary>
        /// 根据id删除单行数据
        /// </summary>
        /// <param name="id">id值</param>
        /// <returns>删除的行数</returns>
        public int delSingle(int id) {
            string sqlstr = "delete from WebLog where WbId = " + id;
            int i = dbHelper.ExcuteNoQuery(sqlstr);
            return i;
        }
    }
}
